package com.aramis.mydytt.ui.user

import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.aramis.mydytt.R
import com.aramis.mydytt.ui.base.BaseViewHolder

/**
 * Description: MainUserAdapter
 * @author lizhidan
 * Created: 2021/6/4
 */
class MainUserAdapter : RecyclerView.Adapter<MainUserAdapter.MainUserAdapterViewHolder>() {
    private val dataList = listOf("我喜欢的", "我下载的")
    var onItemClickListener: ((position: Int) -> Unit)? = null

    class MainUserAdapterViewHolder(parent: ViewGroup) : BaseViewHolder<String>(parent, R.layout.item_main_user) {
        var onItemClickListener: ((position: Int) -> Unit)? = null
        private val textView: TextView = findView(R.id.text_user_item)
        private val lineView: View = findView(R.id.line_user_item)
        private var dataSize = -1

        override fun bind(m: String, position: Int) {
            textView.text = m
            itemView.setOnClickListener { onItemClickListener?.invoke(position) }
            lineView.visibility = if (position == dataSize - 1) View.GONE else View.VISIBLE
        }

        fun setDataSize(dataSize: Int) {
            this.dataSize = dataSize
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MainUserAdapterViewHolder {
        return MainUserAdapterViewHolder(parent)
    }

    override fun onBindViewHolder(holder: MainUserAdapterViewHolder, position: Int) {
        holder.setDataSize(dataList.size)
        holder.onItemClickListener = onItemClickListener
        holder.bind(dataList[position], position)
    }

    override fun getItemCount(): Int = dataList.size
}